package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Year;

/**
 * Created by Administrator on 2016.8.3.
 */
public class Test1605 extends JFrame{
    Text show;
    Button button;

    public Test1605(){
        show=new Text();
        button=new Button();
        this.add(show,BorderLayout.CENTER);
        this.add(button,BorderLayout.SOUTH);
    }

    public static void main(String args[]){
        Test1605 frame=new Test1605();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Welcome to JAVA");
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class Text extends JPanel{
        final JLabel investmentAmount=new JLabel("Investment Amount");
        final JLabel years=new JLabel("Years");
        final JLabel annualInterestRate=new JLabel("Annual Interest Rate");
        final JLabel futureValue=new JLabel("Future Value");

        JTextField textField1;
        JTextField textField2;
        JTextField textField3;
        JTextField textField4;

        public Text(){
            this.setLayout(new GridLayout(4,2));

            textField1=new JTextField(6);
            textField2=new JTextField(6);
            textField3=new JTextField(6);
            textField4=new JTextField(6);
            textField4.setEnabled(false);

            this.add(investmentAmount);
            this.add(textField1);
            this.add(years);
            this.add(textField2);
            this.add(annualInterestRate);
            this.add(textField3);
            this.add(futureValue);
            this.add(textField4);
        }
    }

    private class Button extends JPanel{
        JButton jbt;

        public Button(){
            jbt=new JButton("Calculate");

            jbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double investmentAmount=Double.valueOf(Test1605.this.show.textField1.getText());
                    double years=Double.valueOf(Test1605.this.show.textField2.getText());
                    double monthlyInterestRate=Double.valueOf(Test1605.this.show.textField3.getText())/12;
                    double futureValue;
                    futureValue=investmentAmount*Math.pow(1+monthlyInterestRate/100,years*12);
                    Test1605.this.show.textField4.setText(futureValue+"");
                }
            });

            this.add(jbt,BorderLayout.EAST);
        }
    }
}
